var R = AjaxStack.Reflection, O = AjaxStack.ObjectExt;

function Property(name, value)
{
	this.name = name;
	this.value = value;
}
Property.factoryFn = AjaxStack.sys.createFactoryFn(Property);
Property.extend(AjaxStack.ASObject, null,
	function(p)
	{
		p.getName = function() { return this.name; }
		p.getValue = function() { return this.value; }
		p.toString = function() { return this.name + ": " + this.value; }
	}
);

YAHOO.namespace("ajaxstack");
YAHOO.ajaxstack.ReflectionTests = new YAHOO.tool.TestCase({

	name: "Reflection Tests",

	//--------------------------------------------- 
	// Setup and tear down 
	//---------------------------------------------

	setUp: function()
	{
	},

	tearDown: function()
	{
	},

	//--------------------------------------------- 
	// Tests 
	//---------------------------------------------

	testConvert: function()
	{
		var from = new Property('color', 'black');
		var to = R.convert(from, from.getFactoryFn());
		Assert.areEqual(from.toString(), to.toString());

		var fromUpper = { Name: 'Color', Value: 'Black' };
		to = R.convert(fromUpper, from.getFactoryFn());
		Assert.areEqual(fromUpper.Name, to.name);
		Assert.areEqual(fromUpper.Value, to.value);

		to = R.convert(from, O.factoryFn);
		Assert.areEqual(from.name, to.name);
		Assert.areEqual(from.value, to.value);
		ArrayAssert.itemsAreEqual(['name', 'value'], O.keys(to));

		to = R.convert(fromUpper, O.factoryFn);
		Assert.areEqual(fromUpper.Name, to.Name);
		Assert.areEqual(fromUpper.Value, to.Value);
		ArrayAssert.itemsAreEqual(['Name', 'Value'], O.keys(to));

		to = R.convert(from, O.factoryFn, ['name']);
		Assert.areEqual(from.name, to.name);
		Assert.isUndefined(to.value);
		ArrayAssert.itemsAreEqual(['name'], O.keys(to));
	},

	testConvertAll: function()
	{
		var from = [
			new Property('color', 'black'),
			new Property('width', 200),
			new Property('height', 100)
		];
		var to = R.convertAll(from, Property.factoryFn);
		Assert.areEqual(from.length, to.length);
		for (var i = 0; i < from.length; i++)
		{
			Assert.areEqual(from[i].toString(), to[i].toString());
		}
	},

	testGetField: function()
	{
		var obj = { name: 1, value: 2 };
		var objUpper = { Name: 1, Value: 2 };

		Assert.areEqual('name', R.getField(obj, 'name'));
		Assert.areEqual('Name', R.getField(objUpper, 'name'));

		Assert.areEqual('name', R.getField(obj, 'Name'));
		Assert.areEqual('Name', R.getField(objUpper, 'Name'));
	}

});

